using System.Collections.Generic;
using Super;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
using UnityEngine.UIElements;
#if SPINE_PACKAGE
using Spine.Unity.Editor;
#endif


public class OperateEditor : EditorWindow
{
	public string UIPath = "Assets/FrameWork/Editor/UIToolKit/EnvironmentToolUI.uxml";
	public VisualElement root;

	[MenuItem("Framework/OperateEditor")]
	public static void ShowExample()
	{
		OperateEditor wnd = GetWindow<OperateEditor>();
		wnd.titleContent = new GUIContent("OperateEditor");
		wnd.minSize = new Vector2(700, 300);
		wnd.maxSize = new Vector2(1000, 500);
	}

	public void CreateGUI()
	{
		// Each editor window contains a root VisualElement object
		root = rootVisualElement;

		VisualTreeAsset visualTree = AssetSettingSO.Ins.Operate;
		VisualElement labelFromUXML = visualTree.Instantiate();
		root.Add(labelFromUXML);

		root.Q<Button>("SortingLayerBtn").RegisterCallback<ClickEvent>(evt => SetSortingLayer());
		root.Q<Label>("PersistentPath").text = GlobalUtil.GetPersistentDataPath("");
		root.Q<Button>("OpenPersistentDataBtn").RegisterCallback<ClickEvent>(OnOpenPersistentDataBtnClick);
		root.Q<Button>("CopyPresistnetPathBtn").RegisterCallback<ClickEvent>(OnCopyPresistnetPathBtnClick);
		root.Q<Button>("SpineBtn").RegisterCallback<ClickEvent>(OnSpineBtnClick);
		root.Q<Button>("AddressableBtn").RegisterCallback<ClickEvent>(OnAddressableBtnClick);
	}

	public void SetSortingLayer()
	{
		SortingLayerUtil.AddSortLayerIfNotExist("Value");
		SortingLayerUtil.AddSortLayerIfNotExist("UIBack");
		SortingLayerUtil.AddSortLayerIfNotExist("UINormal");
		SortingLayerUtil.AddSortLayerIfNotExist("UIFront");
		Debug.Log("添加排序层成功");
	}

	public void OnOpenPersistentDataBtnClick(ClickEvent evt)
	{
		string path = GlobalUtil.GetPersistentDataPath("");
		System.Diagnostics.Process.Start(path);
	}

	public void OnCopyPresistnetPathBtnClick(ClickEvent evt)
	{
		string path = GlobalUtil.GetPersistentDataPath("");
		GUIUtility.systemCopyBuffer = path;
		Debug.Log(path + "已复制到剪切板");
	}


	public void OnSpineBtnClick(ClickEvent evt)
	{
#if SPINE_PACKAGE
		SpinePreferences sp = AssetDatabase.LoadAssetAtPath<SpinePreferences>("Assets/Editor/SpineSettings.asset");
		if (sp == null)
		{
			Debug.LogWarning("Assets/Editor/SpineSettings.asset 路径资源不存在");
			return;
		}
		
		Material material;
		material = sp.BlendModeMaterialMultiply;
		if (material == null)
		{
			Debug.LogWarning("BlendModeMaterialMultiply 材质不存在");
		}

		material = sp.BlendModeMaterialAdditive;
		if (material == null)
		{
			Debug.LogWarning("BlendModeMaterialAdditive 材质不存在");
		}

		material = sp.BlendModeMaterialScreen;
		if (material == null)
		{
			Debug.LogWarning("BlendModeMaterialScreen 材质不存在");
		}
		
#else
		Debug.LogWarning("Spine Package 未导入，请导入Spine Package");
#endif
	}

	public void OnAddressableBtnClick(ClickEvent evt)
	{
		// 获取或创建默认的 AddressableAssetSettings
		var settings = AddressableAssetSettingsDefaultObject.Settings;

		if (settings == null)
		{
			// 如果不存在，则创建新的设置
			settings = AddressableAssetSettings.Create(
				"Assets/AddressableAssetsData",
				"AddressableAssetSettings.asset",
				true,
				true);

			Debug.Log("AddressableAssetSettings created successfully.");
			AddressableAssetSettingsDefaultObject.Settings = settings;
		}
		else
		{
			Debug.LogWarning("AddressableAssetSettings already exists.");
		}
	}
}
